2010年计算机等级考试三级上机题库1
已知数据文件IN.dat中存有300个四位数,并已调用读函数Rdata()把这些数存入数组a中,请编写函数primeNum(),其功能是:求出所有这些四位数中素数的个数count,再把所有满足此条件的四位数依次存入数组b中,然后对数组b的四位数按从小到大的顺序进行排序。最后调用写函数Wdata(),把结果输出到OUT.dat文件中。
例如,5591是素数,则该数满足条件存入数组b中,且个数count=count+1。9812是非素数,则该数不满足条件,不存入数组b中,count值也不加1。
注意:部分源程序已经给出。程序中已定义数组a[300]、b[300],已定义变量count。请勿改动主函数main()、读函数Rdata()和写函数Wdata()的内容。
#include <stdio.h>
int a[300], b[300], count = 0;
int isP(int m)
{ int i;
for (i=2; i<m; i++)
if (m%i == 0)
return 0;
return 1;
}
void primeNum()
{
}
void Rdata()
{ FILE *fp;
int i;
fp = fopen("in.dat", "r");
for (i=0; i<300; i++)
fscanf(fp, "%d,", &a[i]);
fclose(fp);
}
void Wdata()
{ FILE *fp;
int i;
fp = fopen("out.dat", "w");
fprintf(fp, "%d\n", count);
for (i=0; i<count; i++)
fprintf(fp, "%d\n", b[i]);
fclose(fp);
}
main()
{ int i;
Rdata();
primeNum();
Wdata();
printf("count=%d\n", count);
for (i=0; i<count; i++)
printf("b[%d]=%d\n", i, b[i]);
}